import { defineStore } from 'pinia'

export const useFarmingStore = defineStore('farming', {
  state: () => ({
    tenancies: [],
    currentTenancy: null,
    crops: [],
    userCrops: [],
    weather: null,
    friends: [],
    currentFriend: null,
    chatHistory: []
  }),

  actions: {
    setCurrentTenancy(tenancy) {
      this.currentTenancy = tenancy
    },

    setCurrentFriend(friend) {
      this.currentFriend = friend
    },

    updateTenancy(tenancyId, data) {
      const index = this.tenancies.findIndex(t => t.tenancyId === tenancyId)
      if (index !== -1) {
        this.tenancies[index] = { ...this.tenancies[index], ...data }
      }
    },

    addUserCrop(crop) {
      this.userCrops.push(crop)
    },

    updateCropStatus(cropId, status) {
      const crop = this.crops.find(c => c.cropId === cropId)
      if (crop) {
        crop.states = status
      }
    },

    // 初始化数据的action
    async initFarmingData() {
      try {
        // 这里可以添加API调用来获取初始数据
        // const response = await getFarmingData()
        // this.tenancies = response.tenancies
        // this.crops = response.crops
        // ...
      } catch (error) {
        console.error('Failed to initialize farming data:', error)
      }
    }
  }
})